﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jb.Domain
{
    /// <summary>
    /// This Call Will Be Used to Contain the Extension Methods of Entity Context Class
    /// </summary>
    public static class ContextExtensions
    {
        /// <summary>
        /// Determines whether the underlying database connection is exist and operational
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool IsConnected(this JbEntities context)
        {
            try
            {   
                int count = context.Countries.Count();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return false;
        }

        #region Get Commonly Used Entities

        /// <summary>
        /// Gets Company object by ID with Address and Contacts object preloaded
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Company GetCompanyByID(this JbEntities context, int id)
        {
            return context.Companies
                .Include("Address")
                .Include("Contacts")
                .FirstOrDefault(P => P.ID == id);
        }

        /// <summary>
        /// Gets a JobSeeker Object by ID with Address and Resume object Preloaded
        /// </summary>
        /// <param name="context"></param>
        /// <param name="jobSeekerId"></param>
        /// <returns></returns>
        public static JobSeeker GetJobSeekerByID(this JbEntities context, int jobSeekerId)
        {
            return context.JobSeekers
                .Include("Address")
                .Include("Resumes")
                .FirstOrDefault(P => P.ID == jobSeekerId);
        }

        /// <summary>
        /// Gets Job By Id with related child objects 
        /// Like Category, City, Company
        /// </summary>
        /// <param name="context"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Job GetJobByID(this JbEntities context, int id)
        {
            return context.Jobs
                .Include("Category")
                .Include("City")
                .Include("Company")
                .FirstOrDefault(P => P.ID == id);
        }
        #endregion

        #region Check for Duplicates
        /// <summary>
        /// Checks Whethear a Given Email is Exist in some certain tables [User, Contact, ComapnyAddress]
        /// <para>
        /// This is Applicable Only when a new Entity is Going to be created.
        /// In Update Mode this method should not be used. Otherwise it will create cirtical bug.        
        /// </para>
        /// </summary>
        /// <param name="context"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public static bool IsDuplicateEmail(this JbEntities context, String email)
        {
            if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, email, true) == 0) != null)
                return true;
            //else if (context.Contacts.FirstOrDefault(P => String.Compare(P.EmailPersonal, email, true) == 0 || String.Compare(P.EmailOffice, email, true) == 0) != null)
            //    return true;
            else if (context.Addresses.FirstOrDefault(P => String.Compare(P.Email, email, true) == 0) != null)
                return true;

            return false;
        }
        /// <summary>
        /// Checks Whethear a Company Name is Duplicate or not
        /// In Update Mode Company ID should be Supplied
        /// </summary>
        /// <param name="context"></param>
        /// <param name="companyName"></param>
        /// <param name="companyID"></param>
        /// <returns></returns>
        public static bool IsDuplicateCompanyName(this JbEntities context, string companyName, int companyID = 0)
        {
            if (companyID == 0)
            {
                if (context.Companies.FirstOrDefault(P => String.Compare(P.Name, companyName, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Companies.FirstOrDefault(P => P.ID != companyID && String.Compare(P.Name, companyName, true) == 0) != null)
                    return true;
            }
            return false;
        }
        /// <summary>
        /// Checks whethear a UserName is already taken or not.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="userName"></param>
        /// <param name="userID">The User ID that will be excluded for duplicate checking. Pass 0 to check in all records</param>
        /// <returns></returns>
        public static bool IsDuplicateUserName(this JbEntities context, string userName, int userID)
        {
            if (userID == 0)
            {
                if (context.Users.FirstOrDefault(P => String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Users.FirstOrDefault(P => P.ID != userID && String.Compare(P.UserName, userName, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks Whether the content title is already exist or not
        /// </summary>
        /// <param name="context"></param>
        /// <param name="contentUrl"></param>
        /// <param name="contentID"></param>
        /// <returns></returns>
        public static bool IsDuplicateContent(this JbEntities context, string contentUrl, int contentID = 0)
        {
            if (contentID == 0)
            {
                if (context.Contents.FirstOrDefault(P => String.Compare(P.Url, contentUrl, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Contents.FirstOrDefault(P => P.ID != contentID && String.Compare(P.Url, contentUrl, true) == 0) != null)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// Checks whether a category URL is duplicate or not
        /// </summary>
        /// <param name="context"></param>
        /// <param name="categoryUrl"></param>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public static bool IsDuplicateCategory(this JbEntities context, string categoryUrl, int categoryID = 0)
        {
            if (categoryID == 0)
            {
                if (context.Categories.FirstOrDefault(P => String.Compare(P.UrlName, categoryUrl, true) == 0) != null)
                    return true;
            }
            else
            {
                if (context.Categories.FirstOrDefault(P => P.ID != categoryID && String.Compare(P.UrlName, categoryUrl, true) == 0) != null)
                    return true;
            }
            return false;
        }
        #endregion Check for Duplicates
    }
}
